#include "tp1107_api.h"

#if (_NETWOK_MODLE_TYPE_ == _TP1107_MODULE_) && (_NETWOK_FUNCTION_ENABLE_)

bool Tp1107BuildEnteringCommandModeMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    // 此方法开销太大，导致ROM空间不足，故采用如下方法
    // sprintf(MessagePacket, "+++\r\n");
    // MessageSize = strlen(MessagePacket);

    // *pSizeOut = MessageSize;
    // return true;

    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildExitingCommandModeMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'X';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildQueryFirmwareVersionNumberMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'V';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'R';
    MessagePacket[MessageSize++] = '?';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildQueryModuleESNMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'U';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = '?';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildGetUTCTimeMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'G';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'M';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildModuleRebootMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'R';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'B';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildFactoryResetMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'D';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'F';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildGetCurrentLinkStateMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'U';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'S';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'S';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildQueryEntryModeConfigurationMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'C';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'F';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'G';
    MessagePacket[MessageSize++] = '?';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildConfiguringAdmissionModeMessage(void *pDataOut, uint16_t *pSizeOut, bool En)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'C';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'F';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'G';
    MessagePacket[MessageSize++] = '=';
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'U';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'C';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'C';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = ',';
    if (En == true)
    {
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'R';
      MessagePacket[MessageSize++] = 'U';
      MessagePacket[MessageSize++] = 'E';
    }
    else
    {
      MessagePacket[MessageSize++] = 'F';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'L';
      MessagePacket[MessageSize++] = 'S';
      MessagePacket[MessageSize++] = 'E';
    }

    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildCheckingNetworkstatusMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'J';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = '?';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildAccessNetworkMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'J';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = 'I';
    MessagePacket[MessageSize++] = 'N';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildDataTransmissionMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t *pDataPacket, uint16_t PacketSize)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL) && (pDataPacket != NULL))
  {
    if ((PacketSize != 0) && ((PacketSize % 2) != 1))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'U';
      MessagePacket[MessageSize++] = 'N';
      MessagePacket[MessageSize++] = 'B';
      MessagePacket[MessageSize++] = 'S';
      MessagePacket[MessageSize++] = 'E';
      MessagePacket[MessageSize++] = 'N';
      MessagePacket[MessageSize++] = 'D';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "%d", PacketSize);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = ',';

      Tp1107HexToHexString(pDataPacket, &MessagePacket[MessageSize], PacketSize);
      MessageSize += PacketSize * 2;

      MessagePacket[MessageSize++] = ',';
      MessagePacket[MessageSize++] = '1';
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildQueryCurrentFrequencyInformationMessage(void *pDataOut, uint16_t *pSizeOut)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'F';
    MessagePacket[MessageSize++] = 'R';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'Q';
    MessagePacket[MessageSize++] = '?';
    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildSearchNetworkTimeoutMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t Timeout)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Timeout >= TP1107_MIX_NETWORK_TIMEOUT) && (Timeout <= TP1107_MAX_NETWORK_TIMEOUT))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'C';
      MessagePacket[MessageSize++] = 'H';
      MessagePacket[MessageSize++] = '_';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'I';
      MessagePacket[MessageSize++] = 'M';
      MessagePacket[MessageSize++] = 'E';
      MessagePacket[MessageSize++] = 'O';
      MessagePacket[MessageSize++] = 'U';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "%d", Timeout);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = ',';
      sprintf(&MessagePacket[MessageSize], "%d", Timeout);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildConfigureSendTimeoutMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t Timeout)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Timeout >= TP1107_MIX_SEND_TIMEOUT) && (Timeout <= TP1107_MAX_SEND_TIMEOUT))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'O';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "%d", Timeout);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildConfiguringAdaptiveEmissionPowerMessage(void *pDataOut, uint16_t *pSizeOut, bool En)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'U';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = '_';
    MessagePacket[MessageSize++] = 'P';
    MessagePacket[MessageSize++] = 'W';
    MessagePacket[MessageSize++] = 'R';
    MessagePacket[MessageSize++] = '=';
    if (En == true)
    {
      MessagePacket[MessageSize++] = '1';
    }
    else
    {
      MessagePacket[MessageSize++] = '0';
    }

    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildConfiguringEmissionPowerMessage(void *pDataOut, uint16_t *pSizeOut, int Power)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Power >= TP1107_MIX_EMISSION_POWER) && (Power <= TP1107_MAX_EMISSION_POWER))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'P';
      MessagePacket[MessageSize++] = 'W';
      MessagePacket[MessageSize++] = 'R';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "%d", Power);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildAutoFrequencyScanningMessage(void *pDataOut, uint16_t *pSizeOut, bool En)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = '+';
    MessagePacket[MessageSize++] = 'A';
    MessagePacket[MessageSize++] = 'U';
    MessagePacket[MessageSize++] = 'T';
    MessagePacket[MessageSize++] = 'O';
    MessagePacket[MessageSize++] = '_';
    MessagePacket[MessageSize++] = 'D';
    MessagePacket[MessageSize++] = 'L';
    MessagePacket[MessageSize++] = 'F';
    MessagePacket[MessageSize++] = 'R';
    MessagePacket[MessageSize++] = 'E';
    MessagePacket[MessageSize++] = 'Q';
    MessagePacket[MessageSize++] = '=';
    if (En == true)
    {
      MessagePacket[MessageSize++] = '1';
    }
    else
    {
      MessagePacket[MessageSize++] = '0';
    }

    MessagePacket[MessageSize++] = '\r';
    MessagePacket[MessageSize++] = '\n';

    *pSizeOut = MessageSize;
    return true;
  }

  return false;
}

bool Tp1107BuildConfiguringWorkingFrequencyMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t Frequency)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Frequency >= TP1107_MIX_FREQUENCY_RANGE) && (Frequency <= TP1107_MAX_FREQUENCY_RANGE))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'F';
      MessagePacket[MessageSize++] = 'R';
      MessagePacket[MessageSize++] = 'E';
      MessagePacket[MessageSize++] = 'Q';
      MessagePacket[MessageSize++] = '=';
      MessagePacket[MessageSize++] = 'D';
      MessagePacket[MessageSize++] = 'L';
      MessagePacket[MessageSize++] = ',';
      sprintf(&MessagePacket[MessageSize], "%d", Frequency);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildConfiguringFrequencyScanningRangeMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t Value)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Value == 1) || (Value == 3) || (Value == 7) || (Value == 15))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'C';
      MessagePacket[MessageSize++] = 'H';
      MessagePacket[MessageSize++] = 'B';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'N';
      MessagePacket[MessageSize++] = 'D';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "%d", Value);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

bool Tp1107BuildConfiguringNetworkSignalThresholdMessage(void *pDataOut, uint16_t *pSizeOut, uint8_t Threshold)
{
  uint16_t MessageSize = 0;
  char *MessagePacket = (char *)pDataOut;

  if ((pDataOut != NULL) && (pSizeOut != NULL))
  {
    if ((Threshold >= TP1107_MIX_NETWORK_SIGNAL_STRENGTH) && (Threshold <= TP1107_MAX_NETWORK_SIGNAL_STRENGTH))
    {
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = '+';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'U';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'O';
      MessagePacket[MessageSize++] = '_';
      MessagePacket[MessageSize++] = 'D';
      MessagePacket[MessageSize++] = 'L';
      MessagePacket[MessageSize++] = 'T';
      MessagePacket[MessageSize++] = 'H';
      MessagePacket[MessageSize++] = 'R';
      MessagePacket[MessageSize++] = '_';
      MessagePacket[MessageSize++] = 'V';
      MessagePacket[MessageSize++] = 'A';
      MessagePacket[MessageSize++] = 'L';
      MessagePacket[MessageSize++] = '=';
      sprintf(&MessagePacket[MessageSize], "-%d", Threshold);
      MessageSize = strlen(MessagePacket);
      MessagePacket[MessageSize++] = '\r';
      MessagePacket[MessageSize++] = '\n';

      *pSizeOut = MessageSize;
      return true;
    }
  }

  return false;
}

uint8_t Tp1107AuthenticationMessageType(const void *pDataIn, uint16_t DataSize)
{
  char *Keyword = NULL;
  char *ParsingMessage = (char *)pDataIn;
  Tp1107ErrorCodeFormat_t ErrorCode = {0};
  uint8_t MessageType = TP1107_RESPOND_ERROR;

  if (pDataIn != NULL)
  {
    if (DataSize > 0)
    {
      MessageType = TP1107_RESPOND_HOST;

      if (strstr(ParsingMessage, "\r\n") != NULL)
      {
        MessageType = TP1107_RESPOND_ERROR;

        if (strstr(ParsingMessage, "OK") != NULL)
        {
          MessageType = TP1107_RESPOND_AT;

          if (strstr(ParsingMessage, "no_connect") != NULL)
          {
            MessageType = TP1107_RESPOND_DISCONNECT;
          }
        }
      }
    }
  }

  return MessageType;
}

Tp1107MessageParsingResult_t Tp1107ParsingEnteringCommandModeMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingExitingCommandModeMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingQueryFirmwareVersionNumberMessage(const void *pDataIn, char *pVersionNumber, char *pReleaseDate, char *pHashValue, char *pModuleModel)
{
  Tp1107VersionFormat_t VersionFormat = {0};
  char *ParsingMessage = (char *)pDataIn, *pKeyword = NULL, *pEndString = NULL;

  if (pDataIn != NULL)
  {
    pEndString = strstr(ParsingMessage, "\r");
    if (pEndString != NULL)
    {
      *pEndString = '\0';

      pKeyword = strtok(ParsingMessage, "_");
      if (pKeyword != NULL)
      {
        if (strlen(pKeyword) <= sizeof(VersionFormat.VersionNumber))
        {
          strcpy(VersionFormat.VersionNumber, pKeyword);
        }

        pKeyword = strtok(NULL, "_");
        if (pKeyword != NULL)
        {
          if (strlen(pKeyword) <= sizeof(VersionFormat.ReleaseDate))
          {
            strcpy(VersionFormat.ReleaseDate, pKeyword);
          }

          pKeyword = strtok(NULL, "_");
          if (pKeyword != NULL)
          {
            if (strlen(pKeyword) <= sizeof(VersionFormat.HashValue))
            {
              strcpy(VersionFormat.HashValue, pKeyword);
            }

            pKeyword = strtok(NULL, "_");
            if (pKeyword != NULL)
            {
              if (strlen(pKeyword) <= sizeof(VersionFormat.ModuleModel))
              {
                strcpy(VersionFormat.ModuleModel, pKeyword);
              }

              if (pVersionNumber != NULL)
              {
                strcpy(pVersionNumber, VersionFormat.VersionNumber);
              }

              if (pReleaseDate != NULL)
              {
                strcpy(pReleaseDate, VersionFormat.ReleaseDate);
              }

              if (pHashValue != NULL)
              {
                strcpy(pHashValue, VersionFormat.HashValue);
              }

              if (pModuleModel != NULL)
              {
                strcpy(pModuleModel, VersionFormat.ModuleModel);
              }

              return TP1107_PARSING_SUCCESS;
            }
          }
        }
      }
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingQueryModuleESNMessage(const void *pDataIn, char *pModule)
{
  Tp1107ModelNumberFormat_t ModelNumberFormat = {0};
  char *pParsingMessage = (char *)pDataIn, *pKeyword = NULL;

  if (pDataIn != NULL)
  {
    pKeyword = strtok(pParsingMessage, "\r");
    if (pKeyword != NULL)
    {
      if (strlen(pKeyword) < sizeof(ModelNumberFormat.Model))
      {
        strcpy(ModelNumberFormat.Model, pKeyword);
      }

      if (pModule != NULL)
      {
        strcpy(pModule, ModelNumberFormat.Model);
      }

      return TP1107_PARSING_SUCCESS;
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingGetUTCTimeMessage(const void *pDataIn, uint16_t *pYear, uint8_t *pMonth, uint8_t *pDate, uint8_t *pHour, uint8_t *pMinute, uint8_t *pSecond)
{
  Tp1107UtcTimeFormat_t UtcTimeFormat = {0};
  char *pParsingMessage = (char *)pDataIn, *pKeyword = NULL, *pEndString = NULL;

  if (pDataIn != NULL)
  {
    pEndString = strstr(pParsingMessage, "\r");
    if (pEndString != NULL)
    {
      *pEndString = '\0';

      pKeyword = strtok(pParsingMessage, "/");
      if (pKeyword != NULL)
      {
        UtcTimeFormat.Year = (uint16_t)atoi(pKeyword);

        pKeyword = strtok(NULL, "/");
        if (pKeyword != NULL)
        {
          UtcTimeFormat.Month = (uint8_t)atoi(pKeyword);

          pKeyword = strtok(NULL, " ");
          if (pKeyword != NULL)
          {
            UtcTimeFormat.Date = (uint8_t)atoi(pKeyword);

            pKeyword = strtok(NULL, ":");
            if (pKeyword != NULL)
            {
              UtcTimeFormat.Hour = (uint8_t)atoi(pKeyword);

              pKeyword = strtok(NULL, ":");
              if (pKeyword != NULL)
              {
                UtcTimeFormat.Minute = (uint8_t)atoi(pKeyword);

                pKeyword = strtok(NULL, ":");
                if (pKeyword != NULL)
                {
                  UtcTimeFormat.Second = (uint8_t)atoi(pKeyword);

                  if (pYear != NULL)
                  {
                    *pYear = UtcTimeFormat.Year;
                  }

                  if (pMonth != NULL)
                  {
                    *pMonth = UtcTimeFormat.Month;
                  }

                  if (pDate != NULL)
                  {
                    *pDate = UtcTimeFormat.Date;
                  }

                  if (pHour != NULL)
                  {
                    *pHour = UtcTimeFormat.Hour;
                  }

                  if (pMinute != NULL)
                  {
                    *pMinute = UtcTimeFormat.Minute;
                  }

                  if (pSecond != NULL)
                  {
                    *pSecond = UtcTimeFormat.Second;
                  }

                  return TP1107_PARSING_SUCCESS;
                }
              }
            }
          }
        }
      }
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingModuleRebootMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingFactoryResetMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingGetCurrentLinkStateMessage(const void *pDataIn, int *pRSSI, uint8_t *pLQI, int *pSNR, uint16_t *pRfChipError)
{
  Tp1107LinkStatusFormat_t LinkStatusFormat = {0};
  char *pParsingMessage = (char *)pDataIn, *pKeyword = NULL;

  if (pDataIn != NULL)
  {
    pKeyword = strtok(pParsingMessage, ":");
    if (pKeyword != NULL)
    {
      pKeyword = strtok(NULL, "\r\n");
      if (pKeyword != NULL)
      {
        LinkStatusFormat.RSSI = atoi(pKeyword);

        pKeyword = strtok(NULL, ":");
        if (pKeyword != NULL)
        {
          pKeyword = strtok(NULL, "\r\n");
          if (pKeyword != NULL)
          {
            LinkStatusFormat.LQI = (uint8_t)atoi(pKeyword);

            pKeyword = strtok(NULL, ":");
            if (pKeyword != NULL)
            {
              pKeyword = strtok(NULL, "\r\n");
              if (pKeyword != NULL)
              {
                LinkStatusFormat.SNR = atoi(pKeyword);

                pKeyword = strtok(NULL, ":");
                if (pKeyword != NULL)
                {
                  pKeyword = strtok(NULL, "\r\n");
                  if (pKeyword != NULL)
                  {
                    LinkStatusFormat.RfChipError = (uint16_t)atoi(pKeyword);

                    if (pRSSI != NULL)
                    {
                      *pRSSI = LinkStatusFormat.RSSI;
                    }

                    if (pLQI != NULL)
                    {
                      *pLQI = LinkStatusFormat.LQI;
                    }

                    if (pSNR != NULL)
                    {
                      *pSNR = LinkStatusFormat.SNR;
                    }

                    if (pRfChipError != NULL)
                    {
                      *pRfChipError = LinkStatusFormat.RfChipError;
                    }

                    return TP1107_PARSING_SUCCESS;
                  }
                }
              }
            }
          }
        }
      }
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingQueryEntryModeConfigurationMessage(const void *pDataIn, uint8_t *pAccessMode)
{
  char *pParsingMessage = (char *)pDataIn;
  Tp1107AccessModeFormat_t AccessModeFormat = {0};

  if (pDataIn != NULL)
  {
    AccessModeFormat.AccessMode = (strstr(pParsingMessage, "TRUE") != NULL) ? true : false;

    if (pAccessMode != NULL)
    {
      *pAccessMode = AccessModeFormat.AccessMode;
      return TP1107_PARSING_SUCCESS;
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringAdmissionModeMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingCheckingNetworkstatusMessage(const void *pDataIn, uint8_t *pAccessStatus)
{
  char *pParsingMessage = (char *)pDataIn;
  Tp1107NetworkAccessStatus_t AccessStatus = TP1107_UNJOINED;

  if (pDataIn != NULL)
  {
    if (strstr(pParsingMessage, "unjoined") != NULL)
    {
      AccessStatus = TP1107_UNJOINED;
    }
    else if (strstr(pParsingMessage, "joining") != NULL)
    {
      AccessStatus = TP1107_JOINING;
    }
    else if (strstr(pParsingMessage, "joined") != NULL)
    {
      AccessStatus = TP1107_JOINED;
    }

    if (pAccessStatus != NULL)
    {
      *pAccessStatus = AccessStatus;
    }

    return TP1107_PARSING_SUCCESS;
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107ParsingAccessNetworkMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingDataTransmissionMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingQueryCurrentFrequencyInformationMessage(const void *pDataIn, float *pUplinkFrequency, float *pDownlinkFrequency)
{
  char *pParsingMessage = (char *)pDataIn, *pKeyword = NULL;
  Tp1107FrequencyFormat_t FrequencyFormat = {0};

  if (pDataIn != NULL)
  {
    pKeyword = strtok(pParsingMessage, ":");
    if (pKeyword != NULL)
    {
      pKeyword = strtok(NULL, "MHz");
      if (pKeyword != NULL)
      {
        FrequencyFormat.UplinkFrequency = strtod(pKeyword, NULL);

        pKeyword = strtok(NULL, ":");
        if (pKeyword != NULL)
        {
          pKeyword = strtok(NULL, "MHz");
          if (pKeyword != NULL)
          {
            FrequencyFormat.DownlinkFrequency = strtod(pKeyword, NULL);

            if (pUplinkFrequency != NULL)
            {
              *pUplinkFrequency = FrequencyFormat.UplinkFrequency;
            }

            if (pDownlinkFrequency != NULL)
            {
              *pDownlinkFrequency = FrequencyFormat.DownlinkFrequency;
            }

            return TP1107_PARSING_SUCCESS;
          }
        }
      }
    }
  }

  return TP1107_PARSING_ERROR;
}

Tp1107MessageParsingResult_t Tp1107PrasingSearchNetworkTimeoutMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfigureSendTimeoutMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringAdaptiveEmissionPowerMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringEmissionPowerMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingAutoFrequencyScanningessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringWorkingFrequencyMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringFrequencyScanningRangeMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

Tp1107MessageParsingResult_t Tp1107ParsingConfiguringNetworkSignalThresholdMessage(const void *pDataIn)
{
  return TP1107_PARSING_SUCCESS;
}

#endif
